﻿using System;
using System.Collections.Generic;
using System.Text;
using DevComponents.AdvTree;
using PPI.Client.Logic.Distribution;
using PPI.Entities.Distribution;
using ColumnHeader = DevComponents.AdvTree.ColumnHeader;

namespace PPI.Controls.Grid
{
    public partial class TreeGridDistribution : TreeGridBase
    {
        public List<EDistributionPole> Poles;
        public DistributionType DistributionType;
         
        public TreeGridDistribution()
        {
            InitializeComponent();

            cmiEdit.Visible = false;
            cmiAction.Visible = false;
            cmiCreateAsset.Visible = false;
            cmiDelete.Visible = true;
            cmiEdit.Visible = true;
            cmiEditWhithOutImages.Visible = false;
            cmiExportToExcel.Visible = true;
            cmiAction.Visible= false;
            cmiAssociateUtility.Visible = false;            
        }

        protected override bool ShowMenu()
        {
            return GetChildId(treeGrid.SelectedNode) != Int32.MinValue;
        }

        protected override void LoadTree()
        {
            treeGrid.Nodes.Clear();
            treeGrid.Columns.Clear();
            m_ChildElementStyle.BackColor = System.Drawing.Color.FromArgb(234, 239, 242);

            ColumnHeader chPole = new ColumnHeader("Pole");
            chPole.Width.Absolute = 100;

            ColumnHeader chSpecie = new ColumnHeader("Specie");
            chSpecie.Width.Absolute = 100;

            ColumnHeader chClass = new ColumnHeader("Class");
            chClass.Width.Absolute = 100;

            ColumnHeader chMap = new ColumnHeader("Map");
            chMap.Width.Absolute = 100;

            ColumnHeader chSection = new ColumnHeader("Section");
            chSection.Width.Absolute = 100;

            ColumnHeader chLenght = new ColumnHeader("Length");
            chLenght.Width.Absolute = 100;

            ColumnHeader chYear = new ColumnHeader("Year");
            chYear.Width.Absolute = 100;

            ColumnHeader chTreatment = new ColumnHeader("Treatment");
            chTreatment.Width.Absolute = 100;

            ColumnHeader chLatitude = new ColumnHeader("Latitude");
            chLatitude.Width.Absolute = 100;

            ColumnHeader chLongitude = new ColumnHeader("Longitude");
            chLongitude.Width.Absolute = 100;

            ColumnHeader chDate = new ColumnHeader("AcquisitionDate");
            chDate.Width.Absolute = 100;

            treeGrid.Columns.Add(chPole);

            if (DistributionType == Entities.Distribution.DistributionType.Inspection)
            {
                ColumnHeader chInspection = new ColumnHeader("Inspection");
                chInspection.Width.Absolute = 100;
                treeGrid.Columns.Add(chInspection);

                ColumnHeader chResult = new ColumnHeader("Result");
                chResult.Width.Absolute = 100;
                treeGrid.Columns.Add(chResult);

                ColumnHeader chElectrical = new ColumnHeader("Electrical");
                chElectrical.Width.Absolute = 100;
                treeGrid.Columns.Add(chElectrical);

                ColumnHeader chNotes = new ColumnHeader("Notes");
                chNotes.Width.Absolute = 100;
                treeGrid.Columns.Add(chNotes);

                ColumnHeader chComments = new ColumnHeader("Comments");
                chComments.Width.Absolute = 100;
                treeGrid.Columns.Add(chComments);
            }

            treeGrid.Columns.Add(chMap);
            treeGrid.Columns.Add(chSection);
            treeGrid.Columns.Add(chSpecie);
            treeGrid.Columns.Add(chClass);
            treeGrid.Columns.Add(chLenght);
            treeGrid.Columns.Add(chYear);
            treeGrid.Columns.Add(chTreatment);
            treeGrid.Columns.Add(chLatitude);
            treeGrid.Columns.Add(chLongitude);
            treeGrid.Columns.Add(chDate);

             foreach (EDistributionPole pole in Poles)
            {
                Node visitNode = CreateStructureNode(pole);
                treeGrid.Nodes.Add(visitNode);
            }
        }

        
        private Node CreateStructureNode(EDistributionPole pole)
        {
            Node poleNode = new Node();

            poleNode.Text = pole.Id.ToString();
            poleNode.DataKey = pole;

            if (DistributionType == Entities.Distribution.DistributionType.Inspection)
            {
                StringBuilder poleNotes = new StringBuilder();

                foreach (ENote note in pole.Notes)
                    poleNotes.Append(note.Name + ", ");

                if (pole.Notes.Count > 0)
                    poleNotes = poleNotes.Remove(poleNotes.ToString().LastIndexOf(","), 1);

                poleNode.Cells.Add(new Cell(pole.InspectionResult.InspectionType.Name));
                poleNode.Cells.Add(new Cell(pole.InspectionResult.Resultype.Name));
                poleNode.Cells.Add(new Cell(pole.InspectionResult.IsElectrical ? "SI" : "NO"));
                poleNode.Cells.Add(new Cell(poleNotes.ToString().Trim()));
                poleNode.Cells.Add(new Cell(pole.InspectionResult.Comments.Trim()));
            }

            poleNode.Cells.Add(new Cell(pole.Map.Name));
            poleNode.Cells.Add(new Cell(pole.Section.Name));
            poleNode.Cells.Add(new Cell(pole.Specie.Name));
            poleNode.Cells.Add(new Cell(pole.Class.Name));
            poleNode.Cells.Add(new Cell(pole.Length.Name));
            if (pole.Year != int.MinValue)
                poleNode.Cells.Add(new Cell(pole.Year.ToString()));
            else
                poleNode.Cells.Add(new Cell(string.Empty));
            poleNode.Cells.Add(new Cell(pole.Treatment.Name));
            poleNode.Cells.Add(new Cell(pole.Latitude.ToString()));
            poleNode.Cells.Add(new Cell(pole.Longitude.ToString()));
            if (pole.AcquisitionDate != DateTime.MinValue)
                poleNode.Cells.Add(new Cell(pole.AcquisitionDate.ToLongDateString()));
            else
                poleNode.Cells.Add(new Cell(string.Empty));

            ColumnHeader chMeterId = new ColumnHeader("MeterID");
            chMeterId.Width.Absolute = 100;

            ColumnHeader chLatitude = new ColumnHeader("Latitude");
            chLatitude.Width.Absolute = 100;

            ColumnHeader chLongitude = new ColumnHeader("Longitude");
            chLongitude.Width.Absolute = 100;

            ColumnHeader chNumber = new ColumnHeader("Number");
            chNumber.Width.Absolute = 100;

            poleNode.NodesColumns.Add(chMeterId);
            poleNode.NodesColumns.Add(chLatitude);
            poleNode.NodesColumns.Add(chLongitude);
            poleNode.NodesColumns.Add(chNumber);

            foreach (EMeter meter in pole.Meters)
            {
                Node meterNode = new Node();
                meterNode.DataKey = meter;
                meterNode.Style = m_ChildElementStyle;
                meterNode.Text = meter.Id.ToString();

                meterNode.Cells.Add(new Cell(meter.Latitude.ToString()));
                meterNode.Cells.Add(new Cell(meter.Longitude.ToString()));
                meterNode.Cells.Add(new Cell(meter.Number));

                poleNode.Nodes.Add(meterNode);
            }
 
            return poleNode;
        }
        
        public void LoadStructures(string connectionString)
        {
            Poles = LPole.GetDistributionProjectPoles(connectionString, DistributionType, int.MinValue);

            LoadTree();
        }

        public void RefreshStructures(List<EDistributionPole> poles)
        {
            Poles = poles;
            LoadTree();
        }

        public void UpdatePole (EDistributionPole pole)
        {
            List<EDistributionPole> poles = LPole.GetDistributionProjectPoles(MobileBase.ConnectionString, DistributionType, pole.Id);

            if (poles.Count > 0) 
            {
                Node poleNode = treeGrid.SelectedNode;
                int index = poleNode.Index;
                treeGrid.Nodes.Remove(poleNode);

                EDistributionPole poleUpdated = poles[0];
                Node newNode = CreateStructureNode(poleUpdated);
                treeGrid.Nodes.Insert(index, newNode);

                Poles[Poles.IndexOf(pole)] = poleUpdated;
                treeGrid.SelectedNode = newNode;
            }
        }

        public void DeletePole(EDistributionPole pole)
        {
            treeGrid.Nodes.Remove(treeGrid.SelectedNode);
        }

        private void cmiExportToExcel_Click(object sender, EventArgs e)
        {
            OnClickedExportToExcel(this, new EventArgs());
        }
        private void OnClickedExportToExcel(object o, EventArgs e)
        {
            // Safely invoke an event:
            if (ClickedExportToExcel != null)
            {
                ClickedExportToExcel(o, e);
            }
        }
        public event EventHandler ClickedExportToExcel;
    }
}
